<template>
  <div class="app-container">
    <div v-if="!dialogInfo.visible">
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="getScheduleJobPage"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :check-box="false"
        :tab-index="true"
      ></table-container>
    </div>
    <quartz-log
      v-if="quartzLogInfo.visible"
      :query="quartzLogInfo.query"
      v-model:showDialog="quartzLogInfo.visible"
    />
    <modify
      v-if="dialogInfo.visible"
      :title="dialogInfo.title[dialogInfo.type]"
      v-model:visible="dialogInfo.visible"
      :tableRow="dialogInfo.tableRow"
    />
  </div>
</template>

<script setup name="quartzManage">
import {
  getScheduleJobPage,
  deleteScheduleJob,
  scheduleScheduleJob,
  rescheduleScheduleJob,
  stopScheduleJob,
  executeScheduleJob,
} from '@/api/schedule.js'
import QuartzLog from './log'
import Modify from './modify'
const { proxy } = getCurrentInstance()
// 过滤相关配置
const filterInfo = ref({
  query: {
    nameLike: '',
    sort: 'create_time desc',
  },
  list: [
    { type: 'button', label: '新增', show: true, event: create },
    {
      type: 'input',
      label: '名称',
      value: 'jobNameLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '任务名称', value: 'jobName' },
    { label: '任务分组', value: 'jobGroup' },
    { label: 'cron表达式', value: 'cronExpression' },
    { label: '执行时间', value: 'execDateTime' },
    { label: '类名', value: 'beanName' },
    { label: '方法名', value: 'methodName' },
    { label: '状态', value: 'statusValue' },
    { label: '描述', value: 'description' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '日志',
        event: log,
        show: proxy.$permission('quartz:log'),
      },
      {
        label: '修改',
        event: update,
        show: proxy.$permission('quartz:update'),
        ifRender(row) {
          return row.status === 'stop'
        },
      },
      {
        label: '发布',
        event: schedule,
        show: proxy.$permission('quartz:publish'),
        ifRender(row) {
          return row.status === 0
        },
      },
      // {
      //   label: '立即执行',
      //   event: execute,
      //   show: proxy.$permission('quartz:execute'),
      //   ifRender(row) {
      //     return row.status === 1
      //   },
      // },
      {
        label: '暂停',
        event: stop,
        show: proxy.$permission('quartz:suspend'),
        ifRender(row) {
          return row.status === 1
        },
      },
      {
        label: '启动',
        event: reschedule,
        show: proxy.$permission('quartz:firing'),
        ifRender(row) {
          return row.status === 2
        },
      },
      {
        label: '删除',
        event: del,
        show: proxy.$permission('quartz:del'),
        ifRender(row) {
          return row.status === 0 || row.status === 2
        },
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = ref({
  title: {
    create: '新增Quartz',
    update: '修改Quartz',
  },
  visible: false,
  type: '',
  tableRow: {},
})
const quartzLogInfo = ref({
  visible: false,
  query: {},
})

watch(
  () => dialogInfo.value.visible,
  (val) => {
    if (!val) {
      getList()
    }
  }
)

onMounted(() => {
  getList()
})

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 创建
function create() {
  dialogInfo.value.type = 'create'
  dialogInfo.value.visible = true
  dialogInfo.value.tableRow = {}
}
// 编辑
function update(data) {
  dialogInfo.value.type = 'update'
  dialogInfo.value.visible = true
  // 显示信息
  dialogInfo.value.tableRow = data
}
function log(row) {
  quartzLogInfo.value.visible = true
  quartzLogInfo.value.query.jobId = row.id
}
function del(row) {
  const data = []
  data.push(row.id)
  proxy.$handleAPI('delete', deleteScheduleJob, data).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function schedule(row) {
  scheduleScheduleJob(row.id).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function stop(row) {
  stopScheduleJob(row.id).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function execute(row) {
  executeScheduleJob(row.id).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function reschedule(row) {
  rescheduleScheduleJob(row.id).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
</script>

<style scoped></style>
